<?php
namespace app\manage\controller;

use think\Db;
class Shop extends Base
{
    public function shopList() {
        $curr_page = input('post.pageIndex',1);
        $perpage = input('post.pageSize',10);
        $search = input('post.search');
        $where = [
            ['del','=',0]
        ];
        if($search) {
            $where[] = ['shop_name|tel','like',"%{$search}%"];
        }
        if($this->userinfo['admin_id'] !== 1) {
            $shop_ids = $this->getMyShopIds($this->userinfo['admin_id']);
            $where[] = ['id','in',$shop_ids];
        }
        try {
            $count = Db::table('mp_shop')->where($where)->count();
            $list = Db::table('mp_shop')->where($where)->limit(($curr_page - 1)*$perpage,$perpage)->select();
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        $ret = [
            'count' => $count,
            'list' => $list
        ];
        return ajax($ret);
    }
//添加门店
    public function shopAdd() {
        $post['admin_id'] = input('post.admin_id');
        $post['shop_name'] = input('post.shop_name');
        $post['leader'] = input('post.leader');
        $post['tel'] = input('post.tel');
        checkPost($post);
        $post['desc'] = input('post.desc');
        $tmp_pic = input('post.pic','');
        try {
            if(!$tmp_pic) {
                return ajax('请上传图片',3);
            }
            $oss_exist = $this->existObject($tmp_pic);
            if($oss_exist !== true) {
                return ajax('图片已失效请重新上传',70);
            }
            $image_move = $this->moveObject($tmp_pic,'upload/shop/');
            if($image_move['code'] == 0) {
                $post['pic'] = $image_move['path'];
            }else {
                return ajax('OSS异常',72);
            }
            Db::table('mp_shop')->insert($post);
        }catch (\Exception $e) {
            if(isset($post['pic'])) {
                $this->deleteObject($post['pic']);
            }
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }
//门店详情
    public function shopDetail() {
        $post['id'] = input('post.id','');
        try {
            $whereshop = [
                ['id','=',$post['id']]
            ];
            $info = Db::table('mp_shop')->where($whereshop)->find();
            if(!$info) {
                return ajax('非法参数',-4);
            }
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax($info);
    }
//修改门店POST
    public function shopMod() {
        $post['admin_id'] = input('post.admin_id');
        $post['shop_name'] = input('post.shop_name');
        $post['leader'] = input('post.leader');
        $post['tel'] = input('post.tel');
        $post['id'] = input('post.id');
        checkPost($post);
        $post['desc'] = input('post.desc');
        $tmp_pic = input('post.pic','');
        try {
            $where = [
                ['id','=',$post['id']]
            ];
            $shop_exist = Db::table('mp_shop')->where($where)->find();
            if(!$shop_exist) {
                return ajax('非法参数',-4);
            }
            $old_pic = $shop_exist['pic'];
            if(!$tmp_pic) {
                return ajax('请上传图片',3);
            }
            $oss_exist = $this->existObject($tmp_pic);
            if($oss_exist !== true) {
                return ajax('图片已失效请重新上传',70);
            }
            $image_move = $this->moveObject($tmp_pic,'upload/shop/');
            if($image_move['code'] == 0) {
                $post['pic'] = $image_move['path'];
            }else {
                return ajax('OSS异常',72);
            }
            Db::table('mp_shop')->where($where)->update($post);
        }catch (\Exception $e) {
            if(isset($post['pic']) && $post['pic'] !== $old_pic) {
                $this->deleteObject($post['pic']);
            }
            return ajax($e->getMessage(),-1);
        }
        if(isset($post['pic']) && $post['pic'] !== $old_pic) {
            $this->deleteObject($old_pic);
        }
        return ajax();
    }
//删除门店
    public function shopDelete() {
        $post['id'] = input('post.id');
        try {
            $whereshop = [
                ['id','=',$post['id']]
            ];
            $exist = Db::table('mp_shop')->where($whereshop)->find();
            if(!$exist) {
                return ajax('非法参数',-1);
            }
            Db::table('mp_shop')->where($whereshop)->update(['del'=>1]);
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }
    //门店状态切换
    public function shopStatus() {
        $post['id'] = input('post.id');
        try {
            $whereshop = [
                ['id','=',$post['id']]
            ];
            $exist = Db::table('mp_shop')->where($whereshop)->find();
            if(!$exist) {
                return ajax('非法参数',-1);
            }
            if($exist['status'] === 1) {
                Db::table('mp_shop')->where($whereshop)->update(['status'=>0]);
            }else {
                Db::table('mp_shop')->where($whereshop)->update(['status'=>1]);
            }
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }
//二维码列表
    public function tableList() {
        $curr_page = input('post.pageIndex',1);
        $perpage = input('post.pageSize',10);
        $shop_id = input('post.shop_id');
        $where = [
            ['t.del','=',0],
            ['s.del','=',0],
        ];
        if($shop_id && $shop_id !== 'all') {
            $where[] = ['t.shop_id','=',$shop_id];
        }
        if($this->userinfo['admin_id'] !== 1) {
            $shop_ids = $this->getMyShopIds($this->userinfo['admin_id']);
            $where[] = ['t.shop_id','in',$shop_ids];
        }
        try {
            $count = Db::table('mp_shop_table')->alias('t')
                ->join('mp_shop s','t.shop_id=s.id','left')
                ->where($where)->count();
            $list = Db::table('mp_shop_table')->alias('t')
                ->join('mp_shop s','t.shop_id=s.id','left')
                ->where($where)
                ->limit(($curr_page - 1)*$perpage,$perpage)
                ->field('t.id,t.shop_id,t.table_no,t.qrcode_value,t.status,s.shop_name')
                ->order(['t.id'=>'DESC'])
                ->select();
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        $ret = [
            'count' => $count,
            'list' => $list
        ];
        return ajax($ret);
    }
//添加二维码
    public function tableAdd() {
        $post['shop_id'] = input('post.shop_id');
        $post['table_no'] = input('post.table_no');
        checkPost($post);
        if($this->userinfo['admin_id'] !== 1) {
            $shop_ids = $this->getMyShopIds($this->userinfo['admin_id']);
            if(!in_array($post['shop_id'],$shop_ids)) {
                return ajax($post,-4);
            }
        }
        try {
            $whereTable = [
                ['del','=',0],
                ['shop_id','=',$post['shop_id']]
            ];
            $table_num = Db::table('mp_shop_table')->where($whereTable)->count('id');
            if($table_num >= 50) {
                return ajax('已达限制数量, 无法添加',41);
            }
            $where = [
                ['shop_id','=',$post['shop_id']],
                ['table_no','=',$post['table_no']]
            ];
            $exist = Db::table('mp_shop_table')->where($where)->field('id')->find();
            if($exist) {
                return ajax($post,49);
            }
            $post['qrcode_value'] = md5($post['shop_id'].'-'.$post['table_no'].config('login_key'));
            Db::table('mp_shop_table')->insert($post);
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }
    //删除门店
    public function tableDelete() {
        $post['id'] = input('post.id');
        try {
            $where = [
                ['id','=',$post['id']]
            ];
            if($this->userinfo['admin_id'] !== 1) {
                $shop_ids = $this->getMyShopIds($this->userinfo['admin_id']);
                $where[] = ['shop_id','in',$shop_ids];
            }
            $exist = Db::table('mp_shop_table')->where($where)->find();
            if(!$exist) {
                return ajax('非法参数',-1);
            }
            Db::table('mp_shop_table')->where($where)->update(['del'=>1]);
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }
    //二维码状态切换
    public function tableStatus() {
        $post['id'] = input('post.id');
        try {
            $where = [
                ['id','=',$post['id']]
            ];
            if($this->userinfo['admin_id'] !== 1) {
                $shop_ids = $this->getMyShopIds($this->userinfo['admin_id']);
                $where[] = ['shop_id','in',$shop_ids];
            }
            $exist = Db::table('mp_shop_table')->where($where)->find();
            if(!$exist) {
                return ajax('非法参数',-1);
            }
            if($exist['status'] === 1) {
                Db::table('mp_shop_table')->where($where)->update(['status'=>0]);
            }else {
                Db::table('mp_shop_table')->where($where)->update(['status'=>1]);
            }
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }

    private function getMyShopIds($admin_id) {
        $where = [
            ['admin_id','=',$admin_id]
        ];
        $shop_ids = Db::table('mp_shop')->where($where)->column('id');
        return $shop_ids;
    }




}